package cn.cqautotest.cqviehelper.utils;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RawRes;

import com.haibin.calendarview.Calendar;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

public class AssetUtils {

    private static final String TAG = AssetUtils.class.getSimpleName();

    @NonNull
    public static Map<String, Calendar> parseSchoolCalendar(Context context, @RawRes int id) {
        Map<String, Calendar> map = new HashMap<>();
        JSONArray schoolCalendarArray = AssetUtils.getProvinceJson(context, id);
        if (schoolCalendarArray != null) {
            try {
                for (int i = 0; i < schoolCalendarArray.length(); i++) {
                    JSONObject oneDay = schoolCalendarArray.optJSONObject(i);
                    String description = oneDay.optString("description");
                    int year = oneDay.optInt("year");
                    int month = oneDay.optInt("month");
                    int day = oneDay.optInt("day");
                    String string = oneDay.optString("color");
                    //Log.d(TAG, "initData: =========> color is " + string);
                    if (!TextUtils.isEmpty(string)) {
                        int color = Color.parseColor(string);
                        //Log.d(TAG, String.format("initData: =====> %s年%s月%s日 颜色是：%s  描述为：%s", year, month, day, string, description));
                        Calendar calendar = getSchemeCalendar(year, month, day, color, description);
                        map.put(calendar.toString(), calendar);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return map;
    }

    @NonNull
    public static Calendar getSchemeCalendar(int year, int month, int day, int color, String text) {
        Calendar calendar = new Calendar();
        calendar.setYear(year);
        calendar.setMonth(month);
        calendar.setDay(day);
        //如果单独标记颜色、则会使用这个颜色
        calendar.setSchemeColor(color);
        calendar.setScheme(text);
        calendar.addScheme(new Calendar.Scheme());
        calendar.addScheme(0xFF008800, "假");
        calendar.addScheme(0xFF008800, "节");
        return calendar;
    }

    /**
     * 获取资产目录下面文件的字符串
     */
    @Nullable
    public static JSONArray getProvinceJson(Context context, @RawRes int id) {
        try {
            InputStream inputStream = context.getApplicationContext().getResources().openRawResource(id);
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[512];
            int length;
            while ((length = inputStream.read(buffer)) != -1) {
                outStream.write(buffer, 0, length);
            }
            outStream.close();
            inputStream.close();
            return new JSONArray(outStream.toString());
        } catch (IOException | JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
}
